package cn.bigcontainer.alt.sys.extension;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.bigcontainer.alt.common.entity.BigMap;
import cn.bigcontainer.alt.common.util.WoKit;
import cn.bigcontainer.alt.meta.dao.MDao;
import cn.bigcontainer.alt.meta.dto.MViewDto;
import cn.bigcontainer.alt.meta.extension.MFormSubmission;
import cn.bigcontainer.alt.sys.mapper.SysXiaoxifenzuYhMapper;
import cn.bigcontainer.alt.sys.mapper.SysXiaoxizhuangtaiMapper;
import cn.bigcontainer.alt.sys.po.SysXiaoxifenzuYh;
import cn.bigcontainer.alt.sys.po.SysXiaoxizhuangtai;

/**
 * 消息创建时时，创建消息状态记录
 * @author jiaod
 * @date 2020年3月31日
 */
@Component
public class SysXiaoxiSubmitExtension implements MFormSubmission {

	@Override
	public MViewDto before(MViewDto fv, Boolean create) {
		return null;
	}

	@Resource
	private MDao dao;
	
	@Resource
	private SysXiaoxizhuangtaiMapper sysXiaoxizhuangtaiMapper;
	
	@Resource
	private SysXiaoxifenzuYhMapper sysXiaoxifenzuYhMapper;
	
	private void createJsrData (Set<String> jsrSet, String jsr, Integer xiaoxiId, String type) {
		if (WoKit.isEmpty(jsr)) {
			return;
		}
		String[] ids = jsr.split(",");
		for (String idString : ids) {
			String[] tableValue = idString.split(":");
			if (tableValue[0].equals("sys_xiaoxifenzu")) {
				String fenzuId = tableValue[1];
				List<SysXiaoxifenzuYh> list = sysXiaoxifenzuYhMapper.selectList(new QueryWrapper<SysXiaoxifenzuYh>().eq("mxiaoxifenzu0_id", fenzuId));
				for (int i = 0; i < list.size(); i++) {
					SysXiaoxifenzuYh sysXiaoxifenzuYh = list.get(i);
					String userId = sysXiaoxifenzuYh.getInverseProperty0Id().toString();
					if (!jsrSet.contains(userId)) {
						createXiaoxizhuangtai(jsrSet, xiaoxiId, userId, type);
					}
				}
				
			} else {
				String userId = tableValue[1];
				if (!WoKit.isEmpty(userId) && !jsrSet.contains(userId)) {
					createXiaoxizhuangtai(jsrSet, xiaoxiId, userId, type);
				}
			}
		}
	}

	private void createXiaoxizhuangtai(Set<String> jsrSet, Integer xiaoxiId, String userId, String mjslx) {
		jsrSet.add(userId);
		SysXiaoxizhuangtai zt = new SysXiaoxizhuangtai();
		Integer id = dao.queryInteger("select ifnull(max(mid), 0) + 1 from sys_xiaoxizhuangtai");
		zt.setMid(id);
		zt.setMjsxx0Id(xiaoxiId);
		zt.setMzt(1);
		zt.setMjsr0Id(WoKit.getInt(userId));
		zt.setMjslx(mjslx);
		sysXiaoxizhuangtaiMapper.insert(zt);
	}
	
	@Override
	public void after(MViewDto fv, Boolean create) {
		String viewId = fv.getId();
		BigMap<String,Object> request = fv.getApp().getObject().getRequest();
		// 接收人
		String jsr = request.getString(viewId + "_r20");
		String csr = request.getString(viewId + "_r30");
		String msr = request.getString(viewId + "_r40");
		Set<String> jsrSet = new HashSet<String>();
		Integer xiaoxiId = request.getInt(viewId + "_mid");
		createJsrData(jsrSet, jsr, xiaoxiId, "1");
		createJsrData(jsrSet, csr, xiaoxiId, "2");
		createJsrData(jsrSet, msr, xiaoxiId, "3");
	}

}
